package com.bear.newsset.dao.sql;

import androidx.annotation.NonNull;

import com.bear.newsset.dao.NewsLikeSqlEntityDao;
import com.bear.newsset.dao.NewsStatusSqlEntityDao;
import com.bear.newsset.dao.SQLAction;
import com.bear.newsset.dao.entity.NewsLikeSqlEntity;
import com.bear.newsset.dao.entity.NewsStatusSqlEntity;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;

public class NewsStatusSQL {
    public enum NEWS_LIKE_TYPE {
        ALL, HISTORY, GIVE_LIKE, COLLECT, SHARE
    }

    private NewsStatusSqlEntityDao mStatusDao;
    private NewsLikeSqlEntityDao mLikeDao;

    private NewsStatusSQL() {
        SQLAction action = SQLAction.getInstance();
        mStatusDao = action.getNewsStatusSqlEntityDao();
        mLikeDao = action.getNewsLikeSqlEntityDao();
    }

    public static NewsStatusSQL getInstance() {
        return NewsStatusSQL.HANDLER.INSTANCE;
    }

    private static final class HANDLER {
        private static final NewsStatusSQL INSTANCE = new NewsStatusSQL();
    }

    /**
     * 插入账号喜欢的新闻信息
     * @param e         entity
     * @return          游标  -2：为均为未选中状态，此时会删除该条数据
     */
    public long insertNewsLikeSql(@NonNull NewsLikeSqlEntity e) {
        Long id = e.getId();
        //均为未选中状态，删除
        if( id != null && !e.getIsGiveLike() && !e.getIsCollect() && !e.getIsShare() ) {
            mLikeDao.deleteByKey( id );
            return -2;
        }
        if( e.getCreateTimeStamp() == 0 ) e.setCreateTimeStamp( System.currentTimeMillis() );
        return mLikeDao.insertOrReplace( e );
    }

    /**
     * 查询新闻的喜欢信息
     * @param newsId    新闻id
     * @return          entity
     */
    @NonNull
    public NewsLikeSqlEntity queryNewsLikeSqlForNewsId(String newsId) {
        NewsLikeSqlEntity e = mLikeDao.queryBuilder()
                .where(NewsLikeSqlEntityDao.Properties.NewsId.eq( newsId ) )
                .unique();
        return e == null ? new NewsLikeSqlEntity() : e;
    }

    /**
     * 查询账号下某条新闻的喜欢状态
     * @param account       账号
     * @param newsId        新闻id
     * @return              喜欢状态
     */
    public NewsLikeSqlEntity queryNewsLikeSqlForAccount(@NonNull String account,
                                                        @NonNull String newsId) {
        return mLikeDao.queryBuilder().where(
                NewsLikeSqlEntityDao.Properties.Account.eq( account ),
                NewsLikeSqlEntityDao.Properties.NewsId.eq( newsId )
        ).unique();
    }

    /**
     * 查询账号喜欢的新闻信息
     * @param account       账号
     * @param type          喜欢类型
     * @return              列表
     */
    @NonNull
    public List<NewsLikeSqlEntity> queryNewsLikeSqlAllForAccount(@NonNull String account,
                                                                 @NonNull NEWS_LIKE_TYPE type) {
        List<NewsLikeSqlEntity> list;
        try {
            list = queryBuildNewsLikeAllForAccount(account, type).list();
        } catch (Exception e) {
            e.printStackTrace();
            list = new ArrayList<>();
        }
        return list;
    }

    /**
     * 查询账号喜欢的新闻数量
     * @param account       账号
     * @param type          喜欢类型
     * @return              数量
     */
    @NonNull
    public long queryNewsLikeSqlCountForAccount(String account, @NonNull NEWS_LIKE_TYPE type) {
        return queryBuildNewsLikeAllForAccount(account, type).count();
    }

    /**
     * 查询账号喜欢的QueryBuilder
     * @param account       账号
     * @param type          喜欢类型
     * @return              QueryBuilder
     */
    @NonNull
    public QueryBuilder<NewsLikeSqlEntity> queryBuildNewsLikeAllForAccount(String account,
                                                                           @NonNull
                                                                           NEWS_LIKE_TYPE type) {
        QueryBuilder<NewsLikeSqlEntity> build;
        WhereCondition where = null;
        switch ( type ) {
            case HISTORY:       //历史
                where = NewsLikeSqlEntityDao.Properties.IsHistory.eq( true );
                break;
            case GIVE_LIKE:     //点赞
                where = NewsLikeSqlEntityDao.Properties.IsGiveLike.eq( true );
                break;
            case COLLECT:       //收藏
                where = NewsLikeSqlEntityDao.Properties.IsCollect.eq( true );
                break;
            case SHARE:         //分享
                where = NewsLikeSqlEntityDao.Properties.IsShare.eq( true );
                break;
        }
        build = mLikeDao.queryBuilder()
                .orderDesc( NewsLikeSqlEntityDao.Properties.CreateTimeStamp );   //从高到低排序
        if( where == null ) {
            build.where( NewsLikeSqlEntityDao.Properties.Account.eq( account ) );
        }else {
            build.where( NewsLikeSqlEntityDao.Properties.Account.eq( account ), where );
        }
//        if( where != null ) build.where( where );
        return build;
    }

    public long insertNewsStatusSql(NewsStatusSqlEntity entity) {
        return mStatusDao.insertOrReplace( entity );
    }

    /**
     * 删除某个类型的喜欢状态(全部状态为false时删除，否则伪移除）
     * @param account   账号
     * @param newsId    新闻id
     * @param type      删除类型
     */
    public void deleteNewsStatusSql(String account, String newsId, NEWS_LIKE_TYPE type) {
        NewsLikeSqlEntity e = queryNewsLikeSqlForAccount( account, newsId );
        switch ( type ) {
            case HISTORY:
                e.setIsHistory( false );
                break;
            case GIVE_LIKE:
                e.setIsGiveLike( false );
                break;
            case COLLECT:
                e.setIsCollect( false );
                break;
            case SHARE:
                e.setIsShare( false );
                break;
        }
        //所有状态为false时移除
        if( !e.getIsHistory() && !e.getIsGiveLike() && !e.getIsCollect() && !e.getIsShare() ) {
            deleteNewsStatusSql(account, newsId);
        }else {
            mLikeDao.update( e );
        }

    }

    /**
     * 删除新闻喜欢状态Sql
     * @param account   账号
     * @param newsId    新闻id
     */
    public void deleteNewsStatusSql(String account, String newsId) {
        NewsLikeSqlEntity e = queryNewsLikeSqlForAccount( account, newsId );
        mLikeDao.deleteByKey( e.getId() );
    }

}
